package com.siwei.mes.controller.system;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.CheckQuickConfig;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.service.CheckQuickConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 快检指标配置相关
 *
 * @author linzi
 * @since 2023-11-25 22:22:19
 */
@Api(value = "快检指标配置")
@RestController
@RequestMapping("checkQuickConfig")
public class CheckQuickConfigController {
    @Resource
    private CheckQuickConfigService checkQuickConfigService;

    @ApiOperation(value = "新增快检指标配置")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) CheckQuickConfig entity) {
        Integer projectCategory = entity.getProjectCategory();
        if (projectCategory.equals(MaterialsEnum.CONCRETE.getCode())) {
            assertTrue(entity.getQuickRules() == null, "快检规则为空");
            /**1-小票数量  2-小票方量  3-车车都检*/
            if (entity.getQuickRules() == 1 || entity.getQuickRules() == 2) {
                assertTrue(entity.getQuickThreshold() == null, "快检阈值为空");
            }
        } else {
            /**如果不是混凝土 需要判断物料信息*/
            assertTrue(StringUtils.isBlank(entity.getProjectName()), "产品名称为空");
            assertTrue(StringUtils.isBlank(entity.getProjectNameCode()), "产品名称code为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecs()), "产品规格为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecsCode()), "产品规格code为空");
        }
        return Json.succ().data(ApiResult.addColumn("id", checkQuickConfigService.create(entity)));
    }

    @ApiOperation(value = "修改快检指标配置")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) CheckQuickConfig entity) {
        Integer projectCategory = entity.getProjectCategory();
        if (projectCategory.equals(MaterialsEnum.CONCRETE.getCode())) {
            assertTrue(entity.getQuickRules() == null, "快检规则为空");
            if (entity.getQuickRules() == 2 || entity.getQuickRules() == 3) {
                assertTrue(entity.getQuickThreshold() == null, "快检阈值为空");
            }
        } else {
            /**如果不是混凝土 需要判断物料信息*/
            assertTrue(StringUtils.isBlank(entity.getProjectName()), "产品名称为空");
            assertTrue(StringUtils.isBlank(entity.getProjectNameCode()), "产品名称code为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecs()), "产品规格为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecsCode()), "产品规格code为空");
        }
        checkQuickConfigService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除快检指标配置")
    @PostMapping("/delete")
    public Json delete(@RequestBody CheckQuickConfig entity) {
        checkQuickConfigService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "修改必检")
    @PostMapping("/updateMust")
    public Json updateMust(@RequestBody CheckQuickConfig entity) {
        assertTrue(entity.getId() == null, "快检指标主键id为空");
        assertTrue(entity.getIsMust() == null, "是否必检为空");
        checkQuickConfigService.updateByPrimaryKeySelective(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取快检指标配置")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<CheckQuickConfig, CheckQuickConfig> query) {
        return Json.succ().data(checkQuickConfigService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取快检指标配置")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(checkQuickConfigService.loadById(id));
    }

    @ApiOperation(value = "获取所有快检指标配置")
    @PostMapping("getAll")
    public Json getAll(@RequestBody CheckQuickConfig entity) {
        return Json.succ().data(ApiResult.addList(checkQuickConfigService.getAll(entity)));
    }

}
